package com.nineclock.message.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.nineclock.common.constant.MessageConstant;
import com.nineclock.common.enums.ResponseEnum;
import com.nineclock.common.exception.NcException;
import com.nineclock.common.threadlocal.CurrentUserHolder;
import com.nineclock.message.config.JPushManager;
import com.nineclock.message.dto.NcMessageDTO;
import com.nineclock.message.dto.SysAllowedJoinCompanyUserDTO;
import com.nineclock.message.enums.MessageTypeEnum;
import com.nineclock.message.pojo.NcMessage;
import com.nineclock.message.service.MessageService;
import com.nineclock.model.pojo.SysCompany;
import com.nineclock.model.pojo.SysCompanyUser;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Service;
import org.springframework.data.mongodb.core.query.Query;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class MessageServiceImpl implements MessageService {

    @Autowired
    JPushManager jPushManager;
    @Autowired
    MongoTemplate mongoTemplate;

    @Override
    public Boolean pushSingleMessage(NcMessage message) throws Exception {

        //封装消息
        wrapperMessage(message);
        //保存推送消息
        mongoTemplate.save(message);
        //推送消息
        return jPushManager.sendNotificationByAlias(message.getTitle(), message.getContent(), message.getTargets(), null);
    }
    //封装
    private void wrapperMessage(NcMessage message) {
        String title = MessageTypeEnum.getEnumByType(message.getMessageType()).getTitle();
        String audience = MessageTypeEnum.getEnumByType(message.getMessageType()).getAudience();

        if (StrUtil.isEmpty(title)) {
            throw new NcException(ResponseEnum.MESSAGE_PARAMS_LOST);
        }

        message.setTitle(audience);
        message.setAudience(audience);
        message.setCreateTime(new Date());
        message.setUseStatus(MessageConstant.STATE_UNREAD);


    }

    @Override
    public List<NcMessage> findMessageRecord(NcMessageDTO messageDTO) {

        Query query = new Query();
        if (messageDTO == null) {
            messageDTO = new NcMessageDTO();
        }
        if (StrUtil.isNotEmpty(messageDTO.getId())) {
            query.addCriteria(Criteria.where("_id").is(messageDTO.getId()));
        }
        if (StrUtil.isNotEmpty(messageDTO.getType())){
            query.addCriteria(Criteria.where("messageType").is(messageDTO.getType()));
        }
        if (messageDTO.getTarget()!=null){
            query.addCriteria(Criteria.where("approveUserId").is(messageDTO.getTarget()));
        }
        if (messageDTO.getUserId()!=null){
            query.addCriteria(Criteria.where("applyUserId").is(messageDTO.getUserId()));
        }

        //升序排列
        query.with(Sort.by(Sort.Direction.ASC, "approveStatue"));

        List<NcMessage> messageList = mongoTemplate.find(query, NcMessage.class);

        return messageList;
    }

    @Override
    public void updateMessageWithApproved(String msgId) {
        Query query = new Query();
        query.addCriteria(Criteria.where("_id").is(msgId));
        Update update = new Update();
        update.set("useStatus",1);
        update.set("approveStatue", 1);
        mongoTemplate.updateFirst(query, update, NcMessage.class);

    }


}
